import 'dart:convert';

import 'package:star_author/config/app.dart';
import 'package:star_author/config/ui_constant.dart';
import 'package:star_author/controller/clipboard_getx_controller.dart';
import 'package:star_author/utils/common_util/log_util.dart';
import 'package:star_author/utils/common_util/toast/custom_toast.dart';
import 'package:star_author/utils/http/api_manager.dart';
import 'package:star_author/utils/http/generate_image_util.dart';
import 'package:star_author/utils/http/http_util.dart';
import 'package:star_author/utils/rouces_util.dart';
import 'package:star_author/utils/statistics/event/backflow_event.dart';
import 'package:star_author/utils/statistics/statistics.dart';
import 'package:star_author/utils/user_util.dart';
import 'package:star_author/views/products/bidder_goods_detail_page/bidder_goods_detaill_page.dart';
import 'package:star_author/widgets/dialog/bottom_sheet.dart';
import 'package:star_author/widgets/empty_view/h_empty_view.dart';
import 'package:star_author/widgets/empty_view/v_empty_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
// import 'package:fluwx/fluwx.dart';
import 'package:get/get.dart';

import '../../../model/backflow.dart';

class ShareIconWidget extends StatefulWidget {
  const ShareIconWidget(
      {Key? key,
      required this.image,
      required this.title,
      required this.id,
      this.shareType = 1,
      this.icon: "ic_good_detail_share_lt",
      this.backflowModal})
      : super(key: key);

  final String image;
  final String title;
  final int id;
  final String icon;
  final BackflowModel? backflowModal;

  /// 1 竞拍详情 2商品详情
  final int? shareType;

  @override
  State<ShareIconWidget> createState() => _ShareIconWidgetState();
}

class _ShareIconWidgetState extends State<ShareIconWidget> {
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        showShareView();
      },
      behavior: HitTestBehavior.opaque,
      child: widget.icon == "ic_share_lt"
          ? Image.asset(
              "images/ic_share_lt.png",
              width: 18.w,
              height: 20.w,
            )
          : RoucesUtil.getAssetsIcon(widget.icon, 30.w),
    );
  }

  void showShareView() {
    showMyBottomSheet(
      Container(
        padding: EdgeInsets.only(
          top: 24.w,
          bottom: 40.w,
        ),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.vertical(top: Radius.circular(12.w)),
          color: Colors.white,
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                // shareItem("微信", "images/ic_share_wx_session_lt.png", () async {
                //   CustomToast.show("分享中...");
                //   double price = double.tryParse(
                //           widget.backflowModal?.confirmedPrice ?? '0') ??
                //       0;
                //   GenerateImageUtil.generate(
                //           widget.shareType ?? 0, widget.image, price)
                //       .then((value) {
                //     String pathStr = "";
                //     String share_event = "";
                //     String user_id_str =
                //         '&user_id=${UserUtil.getUserId(context)}';
                //     if (widget.shareType == 1) {
                //       pathStr =
                //           "/pages/backflow/product-tender-detail/product-tender-detail?id=${widget.id}$user_id_str";
                //       share_event = SystemEvent.fen_xiang_jing_biao;
                //     } else if (widget.shareType == 2) {
                //       pathStr =
                //           "/pages/backflow/product-detail/product-detail?id=${widget.id}$user_id_str";
                //       share_event = SystemEvent.fen_xiang_shang_cheng;
                //     }
                //
                //     LogUtil.v('pathStr$pathStr');
                //
                //     var model = WeChatShareMiniProgramModel(
                //       webPageUrl: "https://dev.huiliu.net/",
                //       miniProgramType: App.miniProgramType,
                //       userName: "gh_797d3bd8cbd1",
                //       //小程序的id
                //       title: "帮我看看这个${widget.backflowModal?.category}咋样",
                //       path: pathStr,
                //       thumbnail: WeChatImage.network(value +
                //           "?x-oss-process=image/resize,w_500/format,heic"),
                //       compressThumbnail: false,
                //     );
                //     Map<String, dynamic> d = {
                //       "backflow_id": widget.backflowModal?.id.toString(),
                //       "serial": widget.backflowModal?.goods['serial'],
                //       "category": widget.backflowModal?.category.toString(),
                //       "owner_id": widget.backflowModal?.userId.toString(),
                //       "share_type": "we_chat"
                //     };
                //     StatisticsUtil.event(share_event, d);
                //
                //     shareToWeChat(model); //这一步是分享
                //     EasyLoading.dismiss();
                //     Get.back();
                //
                //   }).catchError((e) {
                //     CustomToast.show('生成图片失败!');
                //     EasyLoading.dismiss();
                //   });
                // }),
                // HEmptyView(99.w),
                shareItem("复制口令", "images/ic_share_copy_word.png", () {
                  Get.back();
                  String pathStr = "";
                  String share_event = "";
                  if (widget.shareType == 1) {
                    pathStr = BidderGoodsDetailPage.routeName;
                    share_event = SystemEvent.fen_xiang_jing_biao;
                  }
                  Map<String, dynamic> d = {
                    "backflow_id": widget.backflowModal?.id.toString(),
                    "serial": widget.backflowModal?.goods['serial'],
                    "category": widget.backflowModal?.category.toString(),
                    "owner_id": widget.backflowModal?.userId.toString(),
                    "share_type": "copy_word"
                  };
                  StatisticsUtil.event(share_event, d);
                  HttpUtil.commonRequest(context, ApiManager.generate_command,
                      method: HttpUtil.MethodPost,
                      data: {
                        "title": "${widget.title}",
                        "path": "$pathStr?id=${widget.id}"
                      }).then((value) {
                    var resultMap = jsonDecode(value.data["result"]);

                    Clipboard.setData(
                      ClipboardData(
                        text: resultMap["command"],
                      ),
                    ).then((value) {
                      ClipboardGetXController controller =
                          Get.find<ClipboardGetXController>();

                      controller.setLastWord(resultMap["command"]);

                      CustomToast.show(
                        "已复制，快去粘贴！",
                      );
                    });
                  });
                }),
              ],
            ),
            VEmptyView(30.w),
            GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () {
                Get.back();
              },
              child: Container(
                width: 350.w,
                height: 40.w,
                decoration: BoxDecoration(
                  color: UIConstant.colorF5F5,
                  borderRadius: BorderRadius.all(
                    Radius.circular(1000),
                  ),
                ),
                alignment: Alignment.center,
                child: Text(
                  "取消",
                  style: TextStyle(
                    color: UIConstant.color33,
                    fontSize: 14.sp,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget shareItem(String title, String image, Function() callback) {
    return GestureDetector(
      onTap: callback,
      behavior: HitTestBehavior.opaque,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Image.asset(
            image,
            width: 48.w,
            height: 48.w,
          ),
          VEmptyView(8.w),
          Text(
            title,
            style: TextStyle(
              fontSize: 14.sp,
              color: UIConstant.color33,
            ),
          ),
        ],
      ),
    );
  }
}
